/**
 * @description 首页 controller
 */

const { createBlog,getFollowersBlogList } = require('../services/blog')
const { SuccessModel,ErrorModel } = require('../model/ResModel')
const { createBlogFailInfo } = require('../model/Errorinfo')
const xss = require('xss')
const { PAGE_SIZE, REG_FOR_AT_WHO } = require('../conf/constant')
const { getUserInfo } = require('../services/user')
const { createAtRelation } = require('../services/at-relation')

//创建微博
async function create({ userId,content,image }) {
  //分析并收集content中的@用户
  //content 格式如 '哈喽 @李四 - lisi 你好'
  const atUserNameList = []
  content = content.replace(
      REG_FOR_AT_WHO,
      (matchStr, nickName, userName) => {
          // 目的不是 replace 而是获取 userName
          atUserNameList.push(userName)
          return matchStr // 替换不生效，预期
      }
  )
  
  // 根据 @ 用户名查询用户信息
  const atUserList = await Promise.all(
      atUserNameList.map(userName => getUserInfo(userName))
  )
  
  // 根据用户信息，获取用户 id
  const atUserIdList = atUserList.map(user => user.id)

  try {
    //创建微博
    const blog = await createBlog({
      userId,
      content:xss(content),
      image
    })

    //创建 @ 关系
    //blog.id
    //service
    await Promise.all(atUserIdList.map(userId=>{
      createAtRelation(blog.id,userId)
    }))

    return new SuccessModel(blog)
  }catch(err) {
    console.err(err.message,err.stack)
    return new ErrorModel(createBlogFailInfo)
  }
}

//获取首页微博列表
async function getHomeBlogList(userId,pageIndex = 0) {  
  //services
  const result = await getFollowersBlogList({userId,pageIndex,pageSize:PAGE_SIZE})
  const { count,blogList } = result

  //返回
  return new SuccessModel({
    isEmpty:blogList.length === 0,
    blogList,
    pageSize:PAGE_SIZE,
    pageIndex,
    count
  })
}

module.exports = {
  create,
  getHomeBlogList
}